﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using iLangl.BusinessModel.Managers;
using iLangl.Transport.Communication;

namespace iLangl.BusinessModel.Actions.Common
{
	public class RegisterPluginAction : BaseTPAction
	{
		public RegisterPluginAction()
		{
			ResponseContext = new PluginRegistrationResponseContext();
			RequestContext = new ActionResultRequestContext();
		}

		public override bool IsSynced
		{
			get { return true; }
		}

		protected override void _execute()
		{
			PluginRegistration plugRegistration = (PluginRegistration)ResponseContext.GetContext();

			if (plugRegistration == null)
			{
				IAlert alert = Facade.Instance.AlertManager.CreateAlert(this);
				alert.AddAction(this);
				alert.AddError(new Error("Plugin Not Found!", "Required Plugin not Found!", eSeverity.Critical));
				Facade.Instance.AlertManager.RaiseAlert(alert);
			}

			IList<IError> servererrors = new ValidatePluginRegistration().Validate(plugRegistration);

			if (servererrors != null)
			{
				IAlert alert = Facade.Instance.AlertManager.CreateAlert(this, servererrors);
				alert.AddAction(this);
				Facade.Instance.AlertManager.RaiseAlert(alert);
			}


			String pluginLicenseKey = registerAction(plugRegistration);

			ActionResult result = new ActionResult();
			result.Errors = servererrors;
			result.ResultValues["PluginLicenseKey"] = pluginLicenseKey;

			RequestContext.SetContext(result);

			base._execute();

		}

		protected virtual String registerAction(PluginRegistration plugRegistration)
		{
			return PluginManager.Instance.RegisterNewPluginUser(plugRegistration);
		}

	

		protected override string GetName()
		{
			return "RegisterPluginAction";
		}

		protected override void validate()
		{
			
		}


	}
}
